<?php

namespace App\Api\Logic;

use App\Api\Logic\Validate\ApplyStoreValidate;
use App\Exceptions\ValidationException;
use App\Helpers\ResponseEnum;
use App\Models\TongUserApplyStore;

class ApplyStoreLogic extends BaseLogic
{
    /**
     * 店铺申请
     * @param array $params
     * @return void
     */
    public function applyStore(array $params)
    {
        $validate = $this->getValidate();
        if(!$validate->scene('applyStore')->check($params)){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,$validate->getError());
        }
        //查询用户有没有过店铺申请
        $userStoreQuery = TongUserApplyStore::query()->where('u_id',$params['u_id']);
        if($userStoreQuery->exists()){
            $userStoreInfo = $userStoreQuery->first();
            if($userStoreInfo['status']==1){
                throw new ValidationException(ResponseEnum::HTTP_ERROR,'您已提交过店铺申请，请耐心等待审核，请勿重复提交');
            }
            if($userStoreInfo['status']==2){
                throw new ValidationException(ResponseEnum::HTTP_ERROR,'您的店铺申请已通过请勿重复提交');
            }
        }
    }

    /**
     * 获取实力对象
     * @return ApplyStoreValidate
     */
    private function getValidate():ApplyStoreValidate{
        return  new ApplyStoreValidate();
    }
}
